package com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Adapter.CampusShopAllAdapter;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Bean.CampusShopAllBean;
import com.superzqb.asus.superzqbUtil.LoadDialog;
import com.superzqb.asus.superzqbUtil.Url;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.superzqb.asus.Api.ApiParam.book;
import static com.superzqb.asus.Api.ApiParam.maxItem;

/**
 * Created by asus on 2017/1/17.
 */

public class CampusShopOther extends Fragment {
    private Context context;
    private ListView lv;
    private PullRefreshLayout layout;
    private List<CampusShopAllBean> listAll;
    private CampusShopAllAdapter adapter;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    private boolean isButtom=false;
    private int type;
    public CampusShopOther() {
        // Required empty public constructor
    }
    private void mClear() {
        listAll.clear();
        listSize=0;
        pagenum=1;
        isButtom=false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.campus_shop_all, container, false);
        listAll=new ArrayList<>();
        initView(view);
       // Bundle bundle = getArguments();//从activity传过来的Bundle

        adapter = new CampusShopAllAdapter(getActivity(), listAll);
        lv.setAdapter(adapter);

        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        mClear();
                        getLv();
                    }
                }, 2000);
            }
        });
        Integer a=1;
        /*layout.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);*/
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id){
                // TODO Auto-generated method stub
                adapter.changeColor(position);
                adapter.notifyDataSetInvalidated();
                CampusShopAllBean item=(CampusShopAllBean) parent.getItemAtPosition(position);
                Intent intent=new Intent(getActivity(),CampusGoodsDetail.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("goods",  item);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    if (lastItem == listSize) {
                        if(!isButtom) {
                            pagenum++;
                            getLv();
                        }else
                            Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = lv.getLastVisiblePosition();

            }
        });

        return view;
    }

    private void initView(View view) {
        context=getActivity();
        layout= (PullRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        lv=(ListView)view.findViewById(R.id.listView);
        type=getArguments().getInt("type");
    }

    public void getLv() {
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        CampusShopApi api=retrofit.create(CampusShopApi.class);
        Call<List<CampusShopAllBean>> call=api.getGoodsTypeInfo(pagenum,type);
        call.enqueue(new Callback<List<CampusShopAllBean>>() {
            @Override
            public void onResponse(Call<List<CampusShopAllBean>> call, Response<List<CampusShopAllBean>> response) {
                if(response.isSuccessful()){
                    List<CampusShopAllBean> list=response.body();
                    if(list.size()!=0) {
                        listAll.addAll(list);
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);
                        adapter.notifyDataSetChanged();
                        if(list.size()!=maxItem)
                            isButtom=true;
                    }else{
                        isButtom=true;
                    }
                }

            }

            @Override
            public void onFailure(Call<List<CampusShopAllBean>> call, Throwable throwable) {
               // Toast.makeText(getActivity(),throwable.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mClear();
        getLv();
    }

}
